namespace CIS
{
    using CISLib;
    using ComponentArt.Web.UI;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class CompanyEdit : Page
    {
        protected DropDownList areaDdl;
        protected DropDownList catDdl;
        protected TextBox CompanyAddTxb;
        protected TextBox CompanyFaxTxb;
        protected TextBox CompanyNameTxb;
        protected TextBox CompanyTelTxb;
        protected TextBox CompanyWebsiteTxb;
        protected TextBox CompanyZipTxb;
        protected TextBox CurrentNodeNameTxb;
        protected TextBox CurrentNodeTitleTxb;
        protected TextBox CurrentPartTxb;
        protected TextBox CurrentSortTxb;
        protected Label lbArea;
        protected Label lbCat;
        protected Label lbCompanyAdd;
        protected Label lbCompanyFax;
        protected Label lbCompanyName;
        protected Label lbCompanyTel;
        protected Label lbCompanyWebsite;
        protected Label lbCompanyZip;
        protected Label lbCurrentName;
        protected Label lbCurrentNode;
        protected Label lbCurrentNodeNameTitle;
        protected Label lbCurrentNodeTitle;
        protected Label lbCurrentPart;
        protected Label lbCurrentTitle;
        protected Label lbEditerName;
        protected Label lbeg;
        protected Label lbNewName;
        protected Label lbNewNodeNameTitle;
        protected Label lbNewPart;
        protected Label lbNewTitle;
        protected Label lbOrg;
        protected Label lbParent;
        protected Label lbprovince;
        protected Button NewNodeAddBtn;
        protected TextBox NewNodeNameTxb;
        protected TextBox NewNodeSortTxb;
        protected TextBox NewNodeTitleTxb;
        protected TextBox NewPartTxb;
        protected Button NodeDelBtn;
        protected Button NodeSaveBtn;
        protected UserPrincipal Principal = null;
        protected DropDownList provinceDdl;
        protected Button SaveBtn;
        protected ComponentArt.Web.UI.TreeView TreeView1;

        private void areaDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.provinceDdl.DataSource = ManageProvince.GetProvince(new Guid(this.areaDdl.SelectedValue));
            this.provinceDdl.DataTextField = "RegionName";
            this.provinceDdl.DataValueField = "RegionId";
            this.provinceDdl.DataBind();
            this.provinceDdl.Items.RemoveAt(0);
            this.catDdl.DataSource = ManageCAT.GetCAT(new Guid(this.areaDdl.SelectedValue));
            this.catDdl.DataTextField = "CATGroupName";
            this.catDdl.DataValueField = "CATGroupId";
            this.catDdl.DataBind();
            this.catDdl.Items.RemoveAt(0);
        }

        private void InitializeComponent()
        {
            this.areaDdl.SelectedIndexChanged += new EventHandler(this.areaDdl_SelectedIndexChanged);
            this.SaveBtn.Click += new EventHandler(this.SaveBtn_Click);
            this.NodeSaveBtn.Click += new EventHandler(this.NodeSaveBtn_Click);
            this.NodeDelBtn.Click += new EventHandler(this.NodeDelBtn_Click);
            this.NewNodeAddBtn.Click += new EventHandler(this.NewNodeAddBtn_Click);
            this.TreeView1.NodeMoved += new ComponentArt.Web.UI.TreeView.NodeMovedEventHandler(this.TreeView1_NodeMoved);
            this.TreeView1.NodeSelected += new ComponentArt.Web.UI.TreeView.NodeSelectedEventHandler(this.TreeView1_NodeSelected);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFrom();
            this.areaDdl.Items.Clear();
            Area[] area = ManageArea.GetAreaAryViaId(this.Principal.SysUser.UserId);
            for (int i = 0; i < area.Length; i++)
            {
                ListItem tmpListItem = new ListItem();
                tmpListItem.Text = area[i].AreaName;
                tmpListItem.Value = area[i].AreaId.ToString();
                this.areaDdl.Items.Add(tmpListItem);
            }
            this.provinceDdl.DataSource = ManageProvince.GetProvince(new Guid(this.areaDdl.SelectedValue));
            this.provinceDdl.DataTextField = "RegionName";
            this.provinceDdl.DataValueField = "RegionId";
            this.provinceDdl.DataBind();
            this.provinceDdl.Items.RemoveAt(0);
            this.catDdl.DataSource = ManageCAT.GetCAT(new Guid(this.areaDdl.SelectedValue));
            this.catDdl.DataTextField = "CATGroupName";
            this.catDdl.DataValueField = "CATGroupId";
            this.catDdl.DataBind();
            this.catDdl.Items.RemoveAt(0);
            if (base.Request.QueryString["IsModify"] == "True")
            {
                Guid id = new Guid(base.Request.QueryString["CompanyId"]);
                Company company = ManageCompany.GetCompanyViaId(id);
                this.CompanyNameTxb.Text = company.CompanyName;
                this.CompanyAddTxb.Text = company.CompanyAdd;
                this.CompanyTelTxb.Text = company.CompanyTel;
                this.CompanyFaxTxb.Text = company.CompanyFax;
                this.CompanyWebsiteTxb.Text = company.CompanyWebSite;
                this.CompanyZipTxb.Text = company.CompanyZip;
                for (int i = 0; i < this.areaDdl.Items.Count; i++)
                {
                    if (this.areaDdl.Items[i].Value == company.AreaId.ToString())
                    {
                        this.areaDdl.SelectedIndex = i;
                    }
                }
                this.provinceDdl.DataSource = ManageProvince.GetProvince(new Guid(this.areaDdl.SelectedValue));
                this.provinceDdl.DataTextField = "RegionName";
                this.provinceDdl.DataValueField = "RegionId";
                this.provinceDdl.DataBind();
                this.provinceDdl.Items.RemoveAt(0);
                this.catDdl.DataSource = ManageCAT.GetCAT(new Guid(this.areaDdl.SelectedValue));
                this.catDdl.DataTextField = "CATGroupName";
                this.catDdl.DataValueField = "CATGroupId";
                this.catDdl.DataBind();
                this.catDdl.Items.RemoveAt(0);
                for (int i = 0; i < this.catDdl.Items.Count; i++)
                {
                    if (this.catDdl.Items[i].Value == company.CATtId.ToString())
                    {
                        this.catDdl.SelectedIndex = i;
                    }
                }
                for (int i = 0; i < this.provinceDdl.Items.Count; i++)
                {
                    if (this.provinceDdl.Items[i].Value == company.RegionId.ToString())
                    {
                        this.provinceDdl.SelectedIndex = i;
                    }
                }
                this.InitTreeView(new Guid(base.Request.QueryString["CompanyId"]));
                this.lbEditerName.Text = ManageUser.GetUserViaId(company.UserId).LoginName;
                this.NewNodeSortTxb.Text = "99";
                this.CurrentSortTxb.Text = "99";
            }
        }

        private void InitTreeView(Guid companyId)
        {
            Company company = ManageCompany.GetCompanyViaId(companyId);
            this.TreeView1.Nodes.Clear();
            TreeViewNode TvnRoot = new TreeViewNode();
            TvnRoot.Text = company.CompanyName;
            TvnRoot.Value = company.CompanyId.ToString();
            TvnRoot.NavigateUrl = "";
            TvnRoot.Expanded = true;
            this.TreeView1.Nodes.Add(TvnRoot);
            this.TreeRecursion(TvnRoot, companyId);
        }

        private void NewNodeAddBtn_Click(object sender, EventArgs e)
        {
            try
            {
                if ((this.CurrentNodeNameTxb.Text == string.Empty) || (this.lbCurrentNode.Text == string.Empty))
                {
                    base.Response.Write("<script> alert('请选择新节点的父节点！') </script>");
                }
                else if (this.NewNodeNameTxb.Text.Trim() != string.Empty)
                {
                    ManageTree.InsertNewNode(this.NewNodeNameTxb.Text.Trim(), new Guid(this.lbCurrentNode.Text), new Guid(base.Request.QueryString["CompanyId"]), this.NewNodeTitleTxb.Text.Trim(), this.NewPartTxb.Text.Trim(), Convert.ToInt32(this.NewNodeSortTxb.Text.Trim()));
                    this.InitTreeView(new Guid(base.Request.QueryString["CompanyId"]));
                    this.NewNodeNameTxb.Text = string.Empty;
                    this.NewNodeTitleTxb.Text = string.Empty;
                    this.NewPartTxb.Text = string.Empty;
                }
            }
            catch
            {
            }
        }

        private void NodeDelBtn_Click(object sender, EventArgs e)
        {
            if ((this.CurrentNodeNameTxb.Text == string.Empty) || (this.lbParent.Text == string.Empty))
            {
                base.Response.Write("<script> alert('请选择需要删除的节点！') </script>");
            }
            else if (!ManageTree.IsExitNodeChild(new Guid(this.lbCurrentNode.Text)))
            {
                ManageTree.RemoveNodeViaId(new Guid(this.lbCurrentNode.Text));
                this.InitTreeView(new Guid(base.Request.QueryString["CompanyId"]));
                this.NewNodeNameTxb.Text = string.Empty;
                this.CurrentNodeNameTxb.Text = string.Empty;
                this.lbCurrentNode.Text = string.Empty;
                this.lbParent.Text = string.Empty;
            }
            else
            {
                base.Response.Write("<script> alert('下级未空，无法删除！') </script>");
            }
        }

        private void NodeSaveBtn_Click(object sender, EventArgs e)
        {
            try
            {
                ManageTree.UpdateNodeNameAndTitleViaId(this.CurrentNodeNameTxb.Text.Trim(), this.CurrentNodeTitleTxb.Text.Trim(), new Guid(this.lbCurrentNode.Text), this.CurrentPartTxb.Text.Trim(), Convert.ToInt32(this.CurrentSortTxb.Text.Trim()));
                this.InitTreeView(new Guid(base.Request.QueryString["CompanyId"]));
                this.NewNodeNameTxb.Text = string.Empty;
            }
            catch
            {
            }
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.SelectAuth();
            }
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            string strCompanyName = this.CompanyNameTxb.Text.Trim();
            string strCompanyAdd = this.CompanyAddTxb.Text.Trim();
            string strCompanyTel = this.CompanyTelTxb.Text.Trim();
            string strCompanyFax = this.CompanyFaxTxb.Text.Trim();
            string strCompanyWebsite = this.CompanyWebsiteTxb.Text.Trim();
            if (base.Request.QueryString["IsModify"] == "True")
            {
                if (strCompanyName != string.Empty)
                {
                    ManageCompany.UpdateCompany(new Guid(base.Request.QueryString["CompanyId"]), strCompanyName, strCompanyAdd, strCompanyTel, strCompanyFax, strCompanyWebsite, new Guid(this.catDdl.SelectedItem.Value), new Guid(this.provinceDdl.SelectedItem.Value), new Guid(this.areaDdl.SelectedValue), this.CompanyZipTxb.Text.Trim());
                    base.Response.Write("<script>self.close();</script>");
                }
            }
            else if (strCompanyName != string.Empty)
            {
                ManageCompany.InsertNewCompany(strCompanyName, strCompanyAdd, strCompanyTel, strCompanyFax, strCompanyWebsite, new Guid(this.catDdl.SelectedItem.Value), new Guid(this.provinceDdl.SelectedItem.Value), new Guid(this.areaDdl.SelectedValue), this.CompanyZipTxb.Text.Trim(), this.Principal.SysUser.UserId);
                base.Response.Write("<script>self.close();</script>");
            }
        }

        private void SelectAuth()
        {
            if (this.Principal.OwnRole.TypeA == 0)
            {
                this.InitPage();
                this.CompanyNameTxb.ReadOnly = true;
                this.CompanyAddTxb.ReadOnly = true;
                this.CompanyTelTxb.ReadOnly = true;
                this.CompanyFaxTxb.ReadOnly = true;
                this.CompanyWebsiteTxb.ReadOnly = true;
                this.SaveBtn.Visible = false;
                this.TreeView1.DragAndDropEnabled = false;
                this.TreeView1.AutoPostBackOnSelect = false;
                this.lbCurrentNodeTitle.Visible = false;
                this.lbParent.Visible = false;
                this.lbCurrentNodeNameTitle.Visible = false;
                this.CurrentNodeNameTxb.Visible = false;
                this.NodeSaveBtn.Visible = false;
                this.NodeDelBtn.Visible = false;
                this.lbNewNodeNameTitle.Visible = false;
                this.NewNodeNameTxb.Visible = false;
                this.NewNodeAddBtn.Visible = false;
                this.NewNodeTitleTxb.Visible = false;
                this.NewPartTxb.Visible = false;
                this.CurrentNodeTitleTxb.Visible = false;
                this.CurrentPartTxb.Visible = false;
                this.lbNewPart.Visible = false;
                this.lbNewTitle.Visible = false;
                this.lbCurrentName.Visible = false;
                this.lbCurrentPart.Visible = false;
                this.lbCurrentTitle.Visible = false;
                this.lbNewName.Visible = false;
            }
            else if (this.Principal.OwnRole.TypeA == 1)
            {
                this.InitPage();
            }
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/LanguageConfig.xml");
            this.lbCompanyName.Text = ManageSystemConfig.GetSystemSetLanguage("CompanyEdit", "lbCompanyName", language, path);
            this.lbCompanyAdd.Text = ManageSystemConfig.GetSystemSetLanguage("CompanyEdit", "lbCompanyAdd", language, path);
            this.lbCompanyTel.Text = ManageSystemConfig.GetSystemSetLanguage("CompanyEdit", "lbCompanyTel", language, path);
            this.lbCompanyFax.Text = ManageSystemConfig.GetSystemSetLanguage("CompanyEdit", "lbCompanyFax", language, path);
            this.lbCompanyZip.Text = ManageSystemConfig.GetSystemSetLanguage("CompanyEdit", "lbCompanyZip", language, path);
            if (this.Principal.OwnArea[0].AreaId.ToString() == "a7f26634-10a9-4cb1-aa2c-8c91a8dfdd66")
            {
                this.lbprovince.Text = ManageSystemConfig.GetSystemSetLanguage("CompanyEdit", "lbprovinceOverSea", language, path);
            }
            else
            {
                this.lbprovince.Text = ManageSystemConfig.GetSystemSetLanguage("CompanyEdit", "lbprovince", language, path);
            }
            this.lbCat.Text = ManageSystemConfig.GetSystemSetLanguage("CompanyEdit", "lbCat", language, path);
            this.lbCompanyWebsite.Text = ManageSystemConfig.GetSystemSetLanguage("CompanyEdit", "lbCompanyWebsite", language, path);
            this.SaveBtn.Text = ManageSystemConfig.GetSystemSetLanguage("CompanyEdit", "SaveBtn", language, path);
            this.lbCurrentNodeTitle.Text = ManageSystemConfig.GetSystemSetLanguage("CompanyEdit", "lbCurrentNodeTitle", language, path);
            this.lbCurrentNodeNameTitle.Text = ManageSystemConfig.GetSystemSetLanguage("CompanyEdit", "lbCurrentNodeNameTitle", language, path);
            this.lbNewNodeNameTitle.Text = ManageSystemConfig.GetSystemSetLanguage("CompanyEdit", "lbNewNodeNameTitle", language, path);
            this.lbCurrentTitle.Text = ManageSystemConfig.GetSystemSetLanguage("CompanyEdit", "lbCurrentTitle", language, path);
            this.lbNewName.Text = ManageSystemConfig.GetSystemSetLanguage("CompanyEdit", "lbNewName", language, path);
            this.lbCurrentName.Text = ManageSystemConfig.GetSystemSetLanguage("CompanyEdit", "lbCurrentName", language, path);
            this.lbNewTitle.Text = ManageSystemConfig.GetSystemSetLanguage("CompanyEdit", "lbNewTitle", language, path);
            this.lbCurrentPart.Text = ManageSystemConfig.GetSystemSetLanguage("CompanyEdit", "lbCurrentPart", language, path);
            this.lbNewPart.Text = ManageSystemConfig.GetSystemSetLanguage("CompanyEdit", "lbNewPart", language, path);
            this.NodeSaveBtn.Text = ManageSystemConfig.GetSystemSetLanguage("CompanyEdit", "NodeSaveBtn", language, path);
            this.NodeDelBtn.Text = ManageSystemConfig.GetSystemSetLanguage("CompanyEdit", "NodeDelBtn", language, path);
            this.NewNodeAddBtn.Text = ManageSystemConfig.GetSystemSetLanguage("CompanyEdit", "NewNodeAddBtn", language, path);
            this.lbOrg.Text = ManageSystemConfig.GetSystemSetLanguage("CompanyEdit", "lbOrg", language, path);
        }

        private void TreeRecursion(TreeViewNode node, Guid companyId)
        {
            Guid[] childIdAry = ManageTree.GetNodesChildIdAry(new Guid(node.Value), companyId);
            if (childIdAry.Length != 0)
            {
                for (int i = 0; i < childIdAry.Length; i++)
                {
                    TreeViewNode tmpNode = new TreeViewNode();
                    CisTreeNode cisNode = ManageTree.GetNodeViaId(childIdAry[i], companyId);
                    tmpNode.Text = cisNode.NodeName + "(" + cisNode.NodePart + "-" + cisNode.NodeTitle + ")";
                    tmpNode.Value = cisNode.NodeId.ToString();
                    tmpNode.Expanded = true;
                    node.Nodes.Add(tmpNode);
                    this.TreeRecursion(tmpNode, companyId);
                }
            }
        }

        private void TreeView1_NodeMoved(object sender, TreeViewNodeMovedEventArgs e)
        {
            ManageTree.UpdateNodeParentIdViaNodeId(new Guid(e.Node.Value), new Guid(e.Node.ParentNode.Value));
        }

        private void TreeView1_NodeSelected(object sender, TreeViewNodeEventArgs e)
        {
            string tmpStr = e.Node.Text;
            int firstP = tmpStr.IndexOf("(");
            int minP = tmpStr.IndexOf("-");
            if (firstP != -1)
            {
                this.CurrentNodeNameTxb.Text = tmpStr.Substring(0, firstP);
                this.CurrentPartTxb.Text = tmpStr.Substring(firstP + 1, (minP - firstP) - 1);
                this.CurrentNodeTitleTxb.Text = tmpStr.Substring(minP + 1, (tmpStr.Length - minP) - 2);
                CisTreeNode cisTreeNode = ManageTree.GetNodeViaId(new Guid(e.Node.Value), new Guid(base.Request.QueryString["CompanyId"]));
                this.CurrentSortTxb.Text = cisTreeNode.NodeSort.ToString();
            }
            else
            {
                this.CurrentNodeNameTxb.Text = tmpStr;
            }
            this.lbCurrentNode.Text = e.Node.Value;
            if (e.Node.ParentNode == null)
            {
                this.lbParent.Text = "Null";
            }
            else
            {
                this.lbParent.Text = e.Node.ParentNode.Text;
            }
        }
    }
}

